/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-3-18 21:07:49
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#include "rpc_client_base.h"
#include "RpcClientMgr.h"

CRpcClientBase::CRpcClientBase( CRpcClientMgr* pMgr )
    :m_decoder(pMgr)
{
    m_pMgr = pMgr;
}

CRpcClientBase::~CRpcClientBase()
{
    stop_rpc_client();
}

int CRpcClientBase::start_rpc_client( const char* remote_ip, int remote_port )
{
    if(m_pMgr == NULL) return -1;
    m_clifd = m_pMgr->m_netReactor.open_tcp_client(remote_ip, remote_port);
    if(m_clifd < 0)
        return -1;
    m_pMgr->add_rpc_client(this);
    return m_clifd;
}

int CRpcClientBase::stop_rpc_client()
{
    if(m_pMgr) {
        m_pMgr->m_netReactor.close_fd(m_clifd);
        m_pMgr->del_rpc_client(m_clifd);
    }
    return 0;
}

int CRpcClientBase::invoke( const smart::ref_ptr<NetBinaryPdu>& pdu )
{
    if(pdu == NULL) return -1;
    pdu->toStream();
    if(m_pMgr)
        return m_pMgr->m_netReactor.send(m_clifd, pdu->getBinaryStream(), pdu->getBinaryStreamLength());
    return -1;
}

